/*
 *  levelUpGame - GameController.h
 *  Copyright 2011 W. Reckman. All rights reserved.
 */
#ifndef GAME_CONTROLLER_H
#define GAME_CONTROLLER_H

#include <string>
#include <vector>
#include "ofxOscSender.h"
#include "CollisionHandler.h"
#include "GameEventHandler.h"

using std::string;

class LevelUpApp;
class Settings;
class GameModel;

class GameController : public ofEventArgs {
public:
	typedef std::vector<GameModel::PlayerInfo> vec_PlayerInfo;
	
	static const int END_SEQUENCE_TOTAL_STAGES;
	
	mutable EventArgsCollision argsCollision;
	mutable ofEvent<EventArgsCollision> onCollisionDetected;
	
	EventArgsGame argsGameEvent;
	ofEvent<EventArgsGame> gameEvent;
	
	explicit GameController( LevelUpApp& appRef_ );
	~GameController();
	
	void setup();
	void update();
	
	int getQueuedPlayerCount() const;
	
	int getEndSequenceStage() const;
	float getEndSequenceTimer() const;
	
	void setEndSequenceStage( int stage_ );
	void setEndSequenceTimer( float seconds_ );
	
	void requestNewPlayersFromPhotobooth() const;
	void changeGameState( GAME_STATE newState_, bool sendEvent_ = true );
	void sendBalanceChangedEvent() const;
	bool createPlayerFromQueue( int queueIndex_ );
	
	void onPlayerInput( EventArgsJoystick& ev_ );
	void onNewPhotoboothFile( EventArgsPhotoboothFile& ev_ );
	
	
private:
	const Settings& s;
	LevelUpApp& appRef;
	GameModel& game;
	
	string photoboothHostName;
	int photoboothPort;
	
	CollisionHandler collisionHandler;
	GameEventHandler gameEventHandler;
	mutable ofxOscSender oscSender;
	
	GAME_STATE lastGameState;
	mutable bool sendBalanceChangedEventFlag;
	mutable bool sendGameStateEventFlag;
	mutable int sendPrecountMovementEventForPlayer;
	
	int endSequenceStage;
	float endSequenceEndTime;
	
	int lastPlayerIndexAdded;
	vec_PlayerInfo queuedPlayers;
	
	
	bool parseFilename( const string& name, GameModel::PlayerInfo& qp );
	void checkCollisions();
	
	//TEMP
	void constrainPlayersToCanvas();
	bool queuedPlayersEqual( const GameModel::PlayerInfo& p1, const GameModel::PlayerInfo& p2 ) const; //TEMP
};

#endif /* ! GAME_CONTROLLER_H */
